package daos;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import entidades.Paciente;
import exceptions.NoDocumentoException;
import exceptions.NoNameException;

public class PacienteDAO extends GenericDAO<Paciente> {
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	

	public PacienteDAO() {
		super(Paciente.class);
	}

	@SuppressWarnings("unchecked")
	public List<Paciente> buscarPorNomeLista(String nome) {
		Query q = em.createQuery("Select p from Paciente p  where p.nome like '"
				+ nome + "%'", Paciente.class);
		return q.getResultList();
	}
	
	
	@SuppressWarnings("unchecked")
	public Paciente buscarPorNome(String nome) {
		Query q = em.createQuery("Select p from Paciente p  where p.nome like '"
				+ nome + "%'", Paciente.class);
		if (q.getResultList().size() > 0) {
			return (Paciente) q.getResultList().get(0);
		}else{
			return null;
		}
	}
	
	@SuppressWarnings("unchecked")
	public Paciente buscarPorId(long id) {
		Paciente p = super.find(id);
		return p;
	}
	
	@SuppressWarnings("unchecked")
	public Paciente salvar(Paciente p) throws NoNameException, NoDocumentoException {
		if (p.documento == ""){
			throw new NoDocumentoException();
		}
		
		if ((p.nome) == ""){
			throw new NoNameException();
		}
	

		if (p.id > 0) {
			super.update(p);
		}else{
			super.save(p);
		}
		
		super.save(p);
		return p;
	}
}
